'''
Created on 09/04/2009

@author: eh01
'''
from yarra.datastore import dsconstants, dsofactory
from yarra.datastore.dsstub import DSStub
from yarra.event import eventconstants
from yarra.event.event import Event
from yarra.event.eventhandler import BaseEventHandler
from yarra.event.eventmanager import EventManager
import httplib
import os


class HTTPRequestGETIndexHandler(BaseEventHandler):

    def do(self, data):
        dso = data
        http_request = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_HTTP_REQUEST).get_literal()
        lock = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_LOCK).get_literal()                    
        event_type = eventconstants.HTTP_RESPONSE_EVENT_TYPE
        # get the path of the appropriate html template
        ds_stub = DSStub()
        relative_path = None
        if ds_stub.is_hub():
            relative_path = "../../../pages/hub/index.html"
        else:
            relative_path = "../../../pages/im/index.html"
        
        # get the html
        template_path = os.path.join(os.path.dirname(__file__), relative_path)
        file = open(template_path)
        html_template_string = file.read()
        
        # create new dso        
        new_dso = dsofactory.create_dso_for_im_http_response_handler(httplib.OK, 
                                                               'text/html', 
                                                               html_template_string, 
                                                               http_request, 
                                                               lock)            
        event = Event(event_type, new_dso)
        EventManager().add_event(event)